//
//  BIDAboutViewController.m
//  HJGJCommunity
//
//  Created by mal on 2017/6/21.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDAboutViewController.h"
#import <PgyUpdate/PgyUpdateManager.h>

@interface BIDAboutViewController ()
{
    NSString *_curVersion;
    NSString *_downloadURL;
}
@end

@implementation BIDAboutViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"关于";
    _btn.clipsToBounds = YES;
    _btn.layer.cornerRadius = 4.f;
    //
    //获取当前版本
    _curVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
    _versionLabel.text = [[NSString alloc] initWithFormat:@"V %@", _curVersion];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)updateVersion:(id)sender
{
    [[PgyUpdateManager sharedPgyManager] checkUpdateWithDelegete:self selector:@selector(pgyUpdateHandler:)];
}

- (void)pgyUpdateHandler:(NSDictionary*)infoDic
{
    if(!ISNULL(infoDic))
    {
        NSString *versionName = infoDic[@"versionName"];
        _downloadURL = infoDic[@"downloadURL"];
        if([_curVersion isEqualToString:versionName])
        {
            [self showAlertView:@"当前已经是最新版本了" buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
            NSString *strMsg = [[NSString alloc] initWithFormat:@"当前版本:%@, 最新版本:%@, 是否更新", _curVersion, versionName];
            //
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"提示"
                                                                           message:strMsg
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault
                                                                  handler:^(UIAlertAction * action) {
                                                                      [self toUpdate];
                                                                  }];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){
                
            }];
            
            [alert addAction:defaultAction];
            [alert addAction:cancelAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }
    else
    {
        [self showAlertView:@"当前已经是最新版本了" buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
    }
}

#pragma mark - UIAlertViewDelegate
- (void)toUpdate
{
    [[PgyUpdateManager sharedPgyManager] updateLocalBuildNumber];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:_downloadURL]];
}


@end
